//////////////////////////////////////////////////////////////////////////////
//       Copyright (c) 2003 - RealTime Gaming.  All rights reserved.        //
//////////////////////////////////////////////////////////////////////////////

#include <string.h>

#include "lib.h"
#include "econfirm.h"
#include "opts.h"
#include "smtp.h"

#include "tracksql.h"

//////////////////////////////////////////////////////////////////////////////
// constructor
//////////////////////////////////////////////////////////////////////////////
EmailConfirmation::EmailConfirmation( Library::KeywordSubstitutor* pSubstitutor, TrackSQL* sql )
    :Email( pSubstitutor, sql )
{
}

//////////////////////////////////////////////////////////////////////////////
// getEmailMsg
//////////////////////////////////////////////////////////////////////////////
String
EmailConfirmation::getEmailMsg(int32 casinoId, 
							   int32 skinId,
							   const PID& pid,
                               int32 emailId, 
							   SMTP::Header* hdr)
{
    hdr->nameFrom.empty();
	hdr->emailFrom.empty();
	hdr->emailTo.empty();
	hdr->subject.empty();

    String sResult;
	if (sql)
	{       
		sResult = sql->getEmailMsg( casinoId, 
									skinId, 
                                    pid, 
                                    emailId, 
                                    hdr->nameFrom, 
                                    hdr->emailFrom, 
                                    hdr->emailTo,
                                    hdr->emailBCC,
                                    hdr->subject,
                                    &hdr->bEmailAsHtml);

        if (!sResult.isEmpty())
        {           
            // Perform keyword substition
            if ( m_pSubstitutor )
            {
                String sCacheID;
                sCacheID.sprintf( "%d", emailId );
                sResult = m_pSubstitutor->PerformSubstitution( sCacheID, sResult );
            }		
        }
	}

    return sResult;
}

//////////////////////////////////////////////////////////////////////////////
// send
//////////////////////////////////////////////////////////////////////////////
bool8
EmailConfirmation::send(int32 casinoId, int32 skinId, const PID& pid, int32 emailId)
{
    bool8 result = false;
    
    SMTP::Header hdr;
    String sMsg;
    
    // Get e-mail message from database
    sMsg = getEmailMsg(casinoId, skinId, pid, emailId, &hdr);
    
    if ( !sMsg.isEmpty() )
    {
        // Perform keyword substitution
        if ( m_pSubstitutor )
        {
            String sCacheID;
            sCacheID.sprintf( "%d", emailId );
            sMsg = m_pSubstitutor->PerformSubstitution( sCacheID, sMsg );
        }

        // Send it
        SMTP::send(ServerOpts::smtphost, hdr, sMsg);

        result = true;
    }
    
    return result;
}

//////////////////////////////////////////////////////////////////////////////
// sendToReferral
//
// Used by Player-Affiliate program
// to send an email to the player's Referral
//////////////////////////////////////////////////////////////////////////////
bool8
EmailConfirmation::sendToReferral(int32 casinoId, int32 skinId, const PID& pid, const String& sEmailTo )
{
    bool8 result = false;
    
    SMTP::Header hdr;
    String sMsg;
    
    sMsg = getEmailMsg(casinoId, skinId, pid, PLAYER_AFFILIATE, &hdr);

    if ( !sMsg.isEmpty() )
    {
        // Perform keyword substition
        if ( m_pSubstitutor )
        {
            String sCacheID;
            sCacheID.sprintf( "%d", PLAYER_AFFILIATE );
            sMsg = m_pSubstitutor->PerformSubstitution( sCacheID, sMsg );
        }		

        hdr.emailTo = sEmailTo;
        SMTP::send(ServerOpts::smtphost, hdr, sMsg);
    
        result = true;
    }
    
    return result;
}


//////////////////////////////////////////////////////////////////////////////
// enrollListserv
//////////////////////////////////////////////////////////////////////////////
bool8
EmailConfirmation::enrollListserv(int32 casinoId, int32 skinId, const PID& pid)
{
	bool8 result = false;

	SMTP::Header hdr;
	String sMsg;

	sMsg = getEmailMsg(casinoId, skinId, pid, LISTSERV_SUBSCRIBE, &hdr);

    if ( !sMsg.isEmpty() )
    {
        // Perform keyword substition
        if ( m_pSubstitutor )
        {
            String sCacheID;
            sCacheID.sprintf( "%d", LISTSERV_SUBSCRIBE );
            sMsg = m_pSubstitutor->PerformSubstitution( sCacheID, sMsg );
        }		

	    // swap emailFrom and emailTo...request from player to listserv
	    // (nameFrom not used)
	    if ( !sMsg.isEmpty() && !hdr.emailFrom.isEmpty() && !hdr.emailTo.isEmpty() )
	    {
            hdr.nameFrom = hdr.emailFrom;
            hdr.emailFrom = hdr.emailTo;
            hdr.emailTo = hdr.nameFrom;
            hdr.nameFrom.empty();
	    }

	    SMTP::send(ServerOpts::smtphost, hdr, sMsg);
    
        result = true;
    }

	return result;
}